if(!window.mc){
	window.mc = function(){};
}
window.mc.panel = function(){};

mc.panel.init = function(target){
	this.target = target;
	mc.template.load("chat_panel",mc.panel.redraw);
}

mc.panel.redraw = function(){
	mc.panel.target.empty();
	if(mc.setting.clock){
		mc.panel.draw_clock();
	}
	mc.panel.draw_form();
	
	if(mc.setting.trans){
		mc.panel.draw_trans();
	}
	if(mc.setting.smiles){
		mc.panel.draw_smiles();
	}
	//mc.panel.resize();
}
mc.panel.resize = function(){
	var tw = $(window).width();
	var rw = mc.panel.ribbon.parent().width();
	var ary = $("div.ff",mc.panel.target);
	var real = 0;
	for(var i =0; i < ary.length;i++){
		var w = $(ary[i]).width()
		real += w;
	}
	real -= rw;
	tw -= real;
	mc.panel.ribbon.width(tw);
}
mc.panel.tick = function(next){
	$(this).html(mc.ts()).delay(1000).queue(mc.panel.tick);
	next();
}
mc.panel.draw_clock = function(){
	$.tmpl("chat_timer").appendTo(mc.panel.target).queue(mc.panel.tick);
}
mc.panel.draw_form = function(){
	var f = $.tmpl("chat_form").appendTo(this.target);
	this.ribbon = $(f[0].txt)
	var form_send = mc.inputer.init(this.ribbon);
	f.submit(form_send);
}
mc.panel.draw_trans = function(){
	var o = $.tmpl("chat_tran").appendTo(mc.panel.target).filter("div.ff").css({cursor:"pointer"});
	$(o[0]).click(mc.translate(false));
	$(o[1]).click(mc.translate(true));
}
mc.panel.draw_smiles = function(){
	var o = $.tmpl("chat_smile").appendTo(mc.panel.target).filter("div");
	o.filter("div.ff").css({cursor:"pointer"})
		.click(function(){
			var btn = $(this);
			var o = btn.next();
			o.show();
			var offset = btn.offset();
			offset.left -= o.width();
			offset.top -= o.height();
			o.offset(offset);
			return false;
		})
		.next().delegate("img.sm","click",function(){
			var t = $(this);
			var f = t.attr("src").match(/\/(\w+)\.gif/);
			if(f){
				mc.inputer.insert(":" + f[1] + ":");
			}
			t.parent().hide();
			return false;
		}).html(mc.smiles.panel);
}
